﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RestSharp;
using RestSharp.Authenticators;
using XingNet.Web.Communication;
using XingNet.Web.Communication.Base;
using XingNet.Web.Communication.Requests.v1_Users;
using XingNet.Web.Model;
using XingNet.Web.Model.v1_Users;
using Xunit;

namespace XingNet.Web.Communication.Requests
{

 
  public class v1_Users_Request : RequestHandlerBase
  {
    public v1_Users_Request(XingNetTokenHandler _TokenHandler) : base(_TokenHandler) { }

    [JsonProperty(PropertyName = "users")]
    public UserProfile[] UserProfiles { get; set; }

    /// <summary>
    /// Returns all Users found
    /// </summary>
    /// <param name="ID">ID´s seperated by ,</param>
    /// <returns>UserResponse</returns>
    public Users GetUserByID(string[] IDList)
    {
      string IDs = "";
      foreach (string val in IDList)
      {
        IDs += val + ",";
      }
      var request = new RestRequest("/v1/users/" + IDs);
      var client = XingBaseClient.GetClient(TokenHandler);
      var response = client.Execute(request);

      //Verify Result
      Assert.NotNull(response);
      Assert.Equal(HttpStatusCode.OK, response.StatusCode);

      //Return the Result
      Users Result = JsonConvert.DeserializeObject<Users>(response.Content);
      return Result;
    }
    /// <summary>
    /// Returns the Profile for the user logged in
    /// </summary>
    /// <returns>UserResponse</returns>
    public Users GetProfileForLoggedInUser()
    {
      var request = new RestRequest("/v1/users/me");
      var client = XingBaseClient.GetClient(TokenHandler);
      var response = client.Execute(request);

      //Verify Result
      Assert.NotNull(response);
      Assert.Equal(HttpStatusCode.OK, response.StatusCode);

      //Return the Result
      v1_Users.Users Result = JsonConvert.DeserializeObject<Users>(response.Content);
      return Result;
    }
    /// <summary>
    /// Returns the IDCard for the User Logged IN
    /// </summary>
    /// <returns>UserResponse</returns>
    public v1_Users_IDCard GetIDCardForLoggedInUser()
    {
      var request = new RestRequest("/v1/users/me");
      var client = XingBaseClient.GetClient(TokenHandler);
      var response = client.Execute(request);

      //Verify Result
      Assert.NotNull(response);
      Assert.Equal(HttpStatusCode.OK, response.StatusCode);

      //Return the Result
      v1_Users_IDCard Result = JsonConvert.DeserializeObject<v1_Users_IDCard>(response.Content);
      return Result;
    }


    /// <summary>
    /// /v1/users/find_by_emails
    /// Returns the list of users that belong directly to the given list of email addresses. 
    /// The users will be returned in the same order as the corresponding email addresses. 
    /// If addresses are invalid or no user was found, the user will be returned with the value null.
    /// </summary>
    /// <param name="Emails"></param>
    /// <returns></returns>
    public FindByMail FindByMail(string[] Emails, Fields_User.Fields Fields = Fields_User.Fields.none)
    {
      string Params = "emails=";
      foreach (string val in Emails)
      {
        Params += val + ",";
      }

      //If any fields given
      if((Fields & Fields_User.Fields.none) != 0)
      {
        Params += "&User_Fields='";
        Params = Params.Replace(",&", "&");
        Params += Fields_User.GetFieldList(Fields);
      }


      var request = new RestRequest("/v1/users/find_by_emails");
      var client = XingBaseClient.GetClient(TokenHandler, "",XingBaseClient.RequestMode.ProtectedData);
      request.AddParameter("emails", Params);
      var response = client.Execute(request);

      //Verify Result
      Assert.NotNull(response);
      Assert.Equal(HttpStatusCode.OK, response.StatusCode);

      //Return the Result
      FindByMail Result = JsonConvert.DeserializeObject<FindByMail>(response.Content);
      return Result;

    }


  }
}
